package main

import (
	"go_server/game_server/database/mysql"
	redis_core "go_server/game_server/database/redis"
	"go_server/game_server/database/redis/redis_pubsub"
	"go_server/game_server/dataconfig/config_manager"
	"go_server/game_server/event_manager"
	"go_server/game_server/global"
	"go_server/game_server/initializer/api_init"
	"go_server/game_server/initializer/http_server_init"
	"go_server/game_server/initializer/map_data_init"
	"go_server/game_server/initializer/schedule_init"
	"go_server/game_server/initializer/server_data_init"
	"go_server/game_server/util"
	"go_server/game_server/world_manager"
	"go_server/zinx/zconf"
	"go_server/zinx/zdecoder"
	"go_server/zinx/zlog"
	"go_server/zinx/znet"
	"go_server/zinx/zpack"
)



func main() {
	zconf.GlobalObject.Reload(util.ProjectConfigPath)

	mysql.Init()

	redis_core.Init()

	config_manager.LoadAllGameData()

	server_data_init.ServerData.InitServerInfo()

	map_data_init.InitMapGridData()

	schedule_init.StartGameSchedule()

	http_server_init.StartHttpServer()

	s := znet.NewServer()
	api_init.BindApiHandler(s)

	s.SetOnConnStop(global.OnRoleLogoutEvent)
	s.SetDecoder(zdecoder.NewLTV_Little_Decoder())
	s.SetPacket(zpack.NewDataPackLtv())

	s.SetWebsocketAuth(global.ConnectAuthFunc)

	global.SetGmMode(s)

	redis_pubsub.StartPublish()

	s.Serve()


	//==================这里处理服务器关闭事件==========================
	world_manager.WorldMgrObj.RemoveRoleLogoutCallBack()
	s.Stop()
	schedule_init.StopGameSchedule()
	world_manager.WorldMgrObj.SendServerShutdownEvent()
	event_manager.EventManagerObj.Wg.Wait()
	redis_pubsub.PublishStop()
	zlog.CleanZinxLog(zlog.StdZinxLog)
	redis_core.CloseRedisClient()
	server_data_init.ServerData.SaveServerInfo()
}

